<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="Hexo, NexT" />





  <link rel="alternate" href="/atom.xml" title="Likianta Blog" type="application/atom+xml" />






<meta name="description" content="需求（示例）：将首页 - 知乎（换行）https://www.zhihu.com/转换为[首页 - 知乎](https://www.zhihu.com/)">
<meta property="og:type" content="article">
<meta property="og:title" content="使用js脚本将浏览器标题及网址转换为Markdown格式链接">
<meta property="og:url" content="https://likianta.coding.me/2018/0210-212912/index.html">
<meta property="og:site_name" content="Likianta Blog">
<meta property="og:description" content="需求（示例）：将首页 - 知乎（换行）https://www.zhihu.com/转换为[首页 - 知乎](https://www.zhihu.com/)">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://ozurciydg.bkt.clouddn.com/18-2-10/115183.jpg">
<meta property="og:image" content="http://ozurciydg.bkt.clouddn.com/18-2-10/115183.jpg">
<meta property="og:updated_time" content="2018-02-10T14:24:42.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="使用js脚本将浏览器标题及网址转换为Markdown格式链接">
<meta name="twitter:description" content="需求（示例）：将首页 - 知乎（换行）https://www.zhihu.com/转换为[首页 - 知乎](https://www.zhihu.com/)">
<meta name="twitter:image" content="http://ozurciydg.bkt.clouddn.com/18-2-10/115183.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://likianta.coding.me/2018/0210-212912/"/>





  <title>使用js脚本将浏览器标题及网址转换为Markdown格式链接 | Likianta Blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Likianta Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tutorials">
          <a href="/tags/tutorial-catalog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            教程
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://likianta.coding.me/2018/0210-212912/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Likianta">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://ozurciydg.bkt.clouddn.com/18-2-12/72826549.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Likianta Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">使用js脚本将浏览器标题及网址转换为Markdown格式链接</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-01-27T20:34:17+08:00">
                2018-01-27
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/hexo/" itemprop="url" rel="index">
                    <span itemprop="name">Hexo</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1,333
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  6
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><strong>需求（示例）：</strong>将<code>首页 - 知乎（换行）https://www.zhihu.com/</code>转换为<code>[首页 - 知乎](https://www.zhihu.com/)</code></p>
<p><img src="http://ozurciydg.bkt.clouddn.com/18-2-10/115183.jpg" alt=""></p>
<a id="more"></a>
<p>脚本代码如下（详细注解在代码中给出）：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="comment">// Websites2Markdown 1.1</span></span><br><span class="line">    <span class="comment">//</span></span><br><span class="line">    <span class="comment">// 该脚本可以将形如`首页 - 知乎（换行）https://www.zhihu.com/`这样的格式转换为`[首页 - 知乎](https://www.zhihu.com/)`格式，使之符合Markdown链接写法</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">Websites2Markdown</span>(<span class="params">thisObj</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 脚本名称</span></span><br><span class="line">        <span class="keyword">var</span> scriptName = <span class="string">'Websites2Markdown'</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 创建脚本的UI界面</span></span><br><span class="line">        <span class="comment">// 首先初始化控件</span></span><br><span class="line">        <span class="keyword">var</span> btn_1 = <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">var</span> btn_2 = <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">var</span> btn_3 = <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">var</span> edt_1 = <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">var</span> edt_2 = <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">var</span> panel = <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">var</span> win = <span class="literal">null</span>;</span><br><span class="line">        <span class="function"><span class="keyword">function</span> <span class="title">createUI</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="comment">// 初始化位置参数</span></span><br><span class="line">            <span class="keyword">var</span> offset_x = <span class="number">10</span>;</span><br><span class="line">            <span class="keyword">var</span> offset_y = <span class="number">10</span>;</span><br><span class="line">            <span class="keyword">var</span> width = <span class="number">385</span>;</span><br><span class="line">            <span class="keyword">var</span> height = <span class="number">540</span>;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 创建窗口并在窗口中创建面板（panel）</span></span><br><span class="line">            win = (thisObj <span class="keyword">instanceof</span> Panel) ? thisObj : <span class="keyword">new</span> Window(<span class="string">"palette"</span>, scriptName, [<span class="number">0</span>,<span class="number">0</span>,<span class="number">800</span>,<span class="number">600</span>], &#123; <span class="attr">resizeable</span>:<span class="literal">true</span> &#125;);</span><br><span class="line">            panel = win.add(<span class="string">"panel"</span>, [<span class="number">0</span>,<span class="number">0</span>,<span class="number">800</span>,<span class="number">600</span>]);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 开始添加控件</span></span><br><span class="line">            <span class="comment">// `edt_1`是左半边的编辑框，用于粘贴从浏览器获得原始标题及网址格式</span></span><br><span class="line">            <span class="comment">// 第一个参数是控件类型，第二个参数是控件位置，第三个参数是控件中的显示文字，第四个参数是`edt`属性，表示为“允许文本自动换行”、“允许用户回车换行”</span></span><br><span class="line">            edt_1 = panel.add(<span class="string">'edittext'</span>, [offset_x, offset_y, offset_x + width, offset_y + height], <span class="string">''</span>, &#123; <span class="attr">multiline</span>:<span class="literal">true</span>, <span class="attr">wantReturn</span>:<span class="literal">true</span> &#125;);</span><br><span class="line">            </span><br><span class="line">            <span class="comment">// 调整一下位置参数，继续添加右半边的编辑框</span></span><br><span class="line">            <span class="comment">// `edt_2`用于显示转化后的Markdown链接，其控件写法与上面的类似</span></span><br><span class="line">            offset_x = offset_x * <span class="number">2</span> + width;</span><br><span class="line">            edt_2 = panel.add(<span class="string">'edittext'</span>, [offset_x, offset_y, offset_x + width, offset_y + height], <span class="string">''</span>, &#123; <span class="attr">multiline</span>:<span class="literal">true</span>, <span class="attr">wantReturn</span>:<span class="literal">true</span> &#125;);</span><br><span class="line">            </span><br><span class="line">            <span class="comment">// 然后添加底部按钮，目前只制作了按钮1和按钮2</span></span><br><span class="line">            <span class="comment">// `btn_1`用于清空`edt_1`的文本</span></span><br><span class="line">            width = <span class="number">100</span>;</span><br><span class="line">            height = <span class="number">30</span>;</span><br><span class="line">            offset_x = <span class="number">10</span>;</span><br><span class="line">            offset_y = <span class="number">560</span>;</span><br><span class="line">            btn_1 = panel.add(<span class="string">'button'</span>, [offset_x, offset_y, offset_x + width, offset_y + height], <span class="string">'Clear'</span>);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// `btn_2`用于启动转换功能，是本脚本的重点内容</span></span><br><span class="line">            offset_x = <span class="number">350</span>;</span><br><span class="line">            btn_2 = panel.add(<span class="string">'button'</span>, [offset_x, offset_y, offset_x + width, offset_y + height], <span class="string">'Convert'</span>);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// `btn_3`用于复制`edt_2`的结果到剪切板</span></span><br><span class="line">            <span class="comment">// （由于没找到调用系统剪切板的方法，所以该功能在1.0版本暂时搁置了）</span></span><br><span class="line">            <span class="comment">//offset_x = 690;</span></span><br><span class="line">            <span class="comment">//btn_3 = panel.add('button', [offset_x, offset_y, offset_x + width, offset_y + height], 'Copy');</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">// 记住最后一定要让窗口`show()`出来</span></span><br><span class="line">            win.center();</span><br><span class="line">            win.show();</span><br><span class="line">        &#125;</span><br><span class="line">        createUI(); <span class="comment">// 执行以上函数</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 初始值，`content`将被用于下面的转换功能</span></span><br><span class="line">        <span class="keyword">var</span> content = <span class="string">''</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 设置监听事件</span></span><br><span class="line">        btn_1.onClick = onBtn1Click;</span><br><span class="line">        btn_2.onClick = onBtn2Click;</span><br><span class="line">        <span class="comment">//btn_3.onClick = onBtn3Click;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 编写监听事件</span></span><br><span class="line">        <span class="comment">// 按钮1的事件非常简单，只需将左边的编辑框清空就行了</span></span><br><span class="line">        <span class="function"><span class="keyword">function</span> <span class="title">onBtn1Click</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">            edt_1.text = <span class="string">''</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 按钮2的事件是本文的重点，用于转化网址格式</span></span><br><span class="line">        <span class="function"><span class="keyword">function</span> <span class="title">onBtn2Click</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">            edt_2.text = <span class="string">''</span>; <span class="comment">// 首先确保右编辑框是空的</span></span><br><span class="line">            <span class="keyword">var</span> origin_content = edt_1.text; <span class="comment">// 获取左编辑框的文本内容</span></span><br><span class="line">            <span class="keyword">var</span> regex = <span class="built_in">RegExp</span>(<span class="string">'.+'</span>, <span class="string">'g'</span>); <span class="comment">// 声明一个区间变量，这是一个正则表达式</span></span><br><span class="line">            <span class="comment">// 匹配的内容是查找所有（除换行符以外的）字符</span></span><br><span class="line">            <span class="comment">// 相当于每执行一次就返回一行文字，js会自动在第2、3...次执行时读取下一行文本</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">// 开始遍历每行文本，直到正则式再也匹配不到（返回null）时结束</span></span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; content != <span class="literal">null</span>; i++) &#123;</span><br><span class="line">                edt_2.text += content; <span class="comment">// 往右编辑框中**添加**文本</span></span><br><span class="line">                content = regex(origin_content); <span class="comment">// 匹配**一行文本**</span></span><br><span class="line">                <span class="comment">// 接下来判断**这行文本**究竟是标题文字（例如“首页 - 知乎”）还是网址（例如“https://www.zhihu.com/”）</span></span><br><span class="line">                <span class="keyword">if</span> (i % <span class="number">2</span> == <span class="number">0</span>) &#123;</span><br><span class="line">                    <span class="comment">// 偶数行说明这个是标题行</span></span><br><span class="line">                    content = <span class="string">'- ['</span>+ content[<span class="number">0</span>] + <span class="string">']('</span>; <span class="comment">// 使变成“- [首页 - 知乎](”格式</span></span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="comment">// 奇数行说明这个是网址http...</span></span><br><span class="line">                    content = content[<span class="number">0</span>] + <span class="string">')\n'</span>; <span class="comment">// 使转换成“https://www.zhihu.com/)”格式</span></span><br><span class="line">                &#125;</span><br><span class="line">                </span><br><span class="line">                <span class="comment">// 增加一个异常退出机制</span></span><br><span class="line">                <span class="comment">// 当计数器超过500后给出超时警告，并退出循环</span></span><br><span class="line">                <span class="keyword">if</span> (i &gt; <span class="number">500</span>) &#123;</span><br><span class="line">                    alert(<span class="string">'Timeout!'</span>, <span class="string">'Bad return'</span>);</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 按钮3用于复制右编辑框的文字到系统剪切板（暂时还没找到相关方法）</span></span><br><span class="line">        <span class="function"><span class="keyword">function</span> <span class="title">onBtn3Click</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">            </span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    Websites2Markdown(<span class="keyword">this</span>); <span class="comment">// 执行脚本</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>运行效果图：</p>
<p><img src="http://ozurciydg.bkt.clouddn.com/18-2-10/115183.jpg" alt=""></p>
<hr>
<h1 id="改进：将网址标题中的下划线进行转义"><a href="#改进：将网址标题中的下划线进行转义" class="headerlink" title="改进：将网址标题中的下划线进行转义"></a>改进：将网址标题中的下划线进行转义</h1><p>如果我们遇到了这样的网址：<code>方正兰亭黑系列_方正字体_方正字库（换行）http://www.foundertype.com/index.php/FontInfo/index.html?id=216</code></p>
<p>由于Markdown语法中会将下划线作为标记符号，所以网址标题会出现斜体字“<em>方正字体</em>”，为了解决这个问题，我们需要在偶数行（也就是“标题行”）增加一步：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="comment">// Websites2Markdown 1.1</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">Websites2Markdown</span>(<span class="params">thisObj</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        ...</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">function</span> <span class="title">onBtn2Click</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">            ...</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; content != <span class="literal">null</span>; i++) &#123;</span><br><span class="line">                ...</span><br><span class="line">                <span class="keyword">if</span> (i % <span class="number">2</span> == <span class="number">0</span>) &#123;</span><br><span class="line">                    <span class="comment">// 得到网址标题“方正兰亭黑系列_方正字体_方正字库”</span></span><br><span class="line">                    content = <span class="string">'- ['</span>+ content[<span class="number">0</span>] + <span class="string">']('</span>;</span><br><span class="line"></span><br><span class="line">                    <span class="comment">// 使用正则匹配查找“_”，并将其替换为“\_”</span></span><br><span class="line">                    <span class="comment">// 注意js脚本中需要对反斜杠进行转移，也就是双写反斜杠`\\`输出的才是`\`</span></span><br><span class="line">                    content = content.replace(<span class="regexp">/_/g</span>, <span class="string">'\\_'</span>);</span><br><span class="line">                    <span class="comment">// ↑ `/_/g`是一个正则匹配式，表示匹配下划线，“g”是全局模式的意思</span></span><br><span class="line">                    <span class="comment">// ↑ 其等同于`var regex_2 = RegExp('_', 'g');`</span></span><br><span class="line">                &#125;</span><br><span class="line">                ...</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        ...</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    Websites2Markdown(<span class="keyword">this</span>); <span class="comment">// 执行脚本</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
        <div class="post-widgets">
        

        

        
          
          <div id="needsharebutton-postbottom">
            <span class="btn">
              <i class="fa fa-share-alt" aria-hidden="true"></i>
            </span>
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/0201-090934/" rel="next" title="103 如何制作AE脚本的用户交互界面">
                <i class="fa fa-chevron-left"></i> 103 如何制作AE脚本的用户交互界面
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/0131-214604/" rel="prev" title="105 创建本条字幕的开始时间">
                105 创建本条字幕的开始时间 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zNDI4MS8xMDgxOA=="></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="http://ozurciydg.bkt.clouddn.com/18-2-12/72826549.jpg"
                alt="Likianta" />
            
              <p class="site-author-name" itemprop="name">Likianta</p>
              <p class="site-description motion-element" itemprop="description">Android Dev</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">73</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">2</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="http://blog.csdn.net/likianta" target="_blank" title="CSDN">
                      
                        <i class="fa fa-fw fa-crosshairs"></i>CSDN</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:sheerish@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/likianta" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/5441767757/profile?rightmod=1&wvr=6&mod=personinfo" target="_blank" title="Weibo">
                      
                        <i class="fa fa-fw fa-weibo"></i>Weibo</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#改进：将网址标题中的下划线进行转义"><span class="nav-text">改进：将网址标题中的下划线进行转义</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; 2017 &mdash; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Likianta</span>

  
</div>


  <div class="powered-by">
    <i class="fa fa-user-md"></i>
    <span id="busuanzi_container_site_pv">
      Site visitors:<span id="busuanzi_value_site_pv"></span>
    </span>
  </div>

<!--
  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.3</div>

-->



<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">Site words total count:75.2k</span>
</div>
        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  




	





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  












  





  

  

  

  
  
  
  <link rel="stylesheet" href="/lib/needsharebutton/needsharebutton.css">

  
  
  <script src="/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "box";
      
          pbOptions.boxForm = "horizontal";
      
          pbOptions.position = "bottomCenter";
      
          pbOptions.networks = "Weibo,Wechat,Douban,QQZone,Evernote";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
  </script>

  

  

  

  

</body>
</html>
